package com.example.redis.demo.configuration;

import lombok.Data;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.temporal.ChronoUnit;


/**
 * @description:todo
 * @auth yangzhiwei
 * @date 2021-07-02 14:34
 */
@Data
@Configuration
public class RedisConfig {
    //RedisAutoConfiguration redisAutoConfiguration;

    //这里 Springboot的配置会忽略 RedisProperities内的
    //@ConfigurationProperties注解
    @Bean("redisProperity1")
    @ConfigurationProperties(prefix = "spring.redis.project1")
    public RedisProperties getProject1Properity1() {
        return new RedisProperties();
    }

    @Bean("redisProperity2")
    @ConfigurationProperties(prefix = "spring.redis.project2")
    public RedisProperties getProject1Properity2() {
        return new RedisProperties();
    }


   // 2.根据properity 构建 RedisFactory
   @Bean("lettuceConnectionFactory1")
    public LettuceConnectionFactory getLettuceConnectionFactory1(@Qualifier("redisProperity1") RedisProperties redisProperties) {
        return configConnectionFactory(redisProperties);

    }
    // 2.根据properity 构建 RedisFactory
    @Bean("lettuceConnectionFactory2")
    public LettuceConnectionFactory getLettuceConnectionFactory2(@Qualifier("redisProperity2") RedisProperties redisProperties) {
        return configConnectionFactory(redisProperties);
    }

    // 拿到template
    @Bean("lettuceTemplate1")
    public RedisTemplate restTemplate1(@Qualifier("lettuceConnectionFactory1") RedisConnectionFactory factory) {
       RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
       return redisTemplate;
    }




    // spring默认采用defaultSerializer = new JdkSerializationRedisSerializer();
    @Bean("lettuceTemplate2")
    public RedisTemplate redisTemplate(@Qualifier("lettuceConnectionFactory2") RedisConnectionFactory factory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        return redisTemplate;
    }


    private LettuceConnectionFactory configConnectionFactory(RedisProperties property) {
        // 1.Redis基础信息配置
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();

        configuration.setDatabase(property.getDatabase());
        configuration.setPassword(property.getPassword());
        configuration.setPort(property.getPort());
        configuration.setUsername(property.getUsername());
        configuration.setHostName(property.getHost());
        // 2.连接池配置
        RedisProperties.Pool lettucePool  = property.getLettuce().getPool();
        GenericObjectPoolConfig pool = new GenericObjectPoolConfig();
        if (lettucePool != null) {
            pool.setMinIdle(lettucePool.getMinIdle());
            pool.setMaxTotal(lettucePool.getMaxActive());
            pool.setMaxWaitMillis(lettucePool.getMaxWait().get(ChronoUnit.MILLIS));
            pool.setMaxIdle(lettucePool.getMaxIdle());
        }

        LettuceClientConfiguration clientConfiguration =
                LettucePoolingClientConfiguration.builder().poolConfig(pool).build();

        // 连接工厂配置
        return new LettuceConnectionFactory(configuration, clientConfiguration);
    }


//    // 3.根据factory 构建 template
//    public RedisTemplate<Object, Object> redisTemplate(@Qualifier("") RedisConnectionFactory redisConnectionFactory) {
//        RedisTemplate<Object, Object> template = new RedisTemplate<>();
//        template.setConnectionFactory(redisConnectionFactory);
//        return template;
//    }

}
